clear all;% removes all variables, globals, functions and MEX links.
close all;% closes all the open figure windows.
clc;% clears the command window and homes the cursor.

files = dir('C:\Users\XRQ\Desktop\XRQ-Thesis-CAS-2016\program\segmentation2\gesture_triangle_acceleration_data\triangle9');% list directory

for i = 1 : length(files)
    [acceleration_maf_x, acceleration_maf_y, sample_time] = acceleration_acquisition(strcat('C:\Users\XRQ\Desktop\XRQ-Thesis-CAS-2016\program\segmentation2\gesture_triangle_acceleration_data\', files(i).name), 0.02);

%     my_fft(acceleration_maf_x, strcat('C:\Users\XRQ\Desktop\Hand Gesture Recognition\program\gesture_segmentation\figures\', files(i).name));
    
    figure;
    plot(sample_time, acceleration_maf_x, '-', sample_time, acceleration_maf_y, '--', 'LineWidth', 2);
    axis([0 4 -1.5 1.5]);
%     grid on;
    xlabel('Time (s)');
    ylabel('Acceleration (g)');
    legend('Ax', 'Ay');
    hold on;
    
    previous_acc_x = 0;
    current_acc_x = 0;
    start_point_x = 0;
    end_point_x = 0;
    max_index_x = 0;
    min_index_x = 0;
    for i = 1 : length(acceleration_maf_x)     
        previous_acc_x = current_acc_x;
        current_acc_x = acceleration_maf_x(i);
        
        if (acceleration_maf_x(i) > 0.25)
            if (previous_acc_x > current_acc_x)&&(max_index_x == 0)
                max_index_x = i - 1
                plot(max_index_x*0.02, acceleration_maf_x(max_index_x), 'ro', 'Markersize', 10);
            end
        elseif(acceleration_maf_x(i) < -0.25)
            if (previous_acc_x < current_acc_x)&&(min_index_x == 0)
                min_index_x = i - 1
                plot(min_index_x*0.02, acceleration_maf_x(min_index_x), 'ro', 'Markersize', 10);
            end
        else
            max_index_x = 0;
            min_index_x = 0;
        end
    end
    
    previous_acc_y = 0;
    current_acc_y = 0;
    start_point_y = 0;
    end_point_y = 0;
    max_index_y = 0;
    min_index_y = 0;
    for i = 1 : length(acceleration_maf_y)     
        previous_acc_y = current_acc_y;
        current_acc_y = acceleration_maf_y(i);
        
        if (acceleration_maf_y(i) > 0.25)
            if (previous_acc_y > current_acc_y)&&(max_index_y == 0)
                max_index_y = i - 1
                plot(max_index_y*0.02, acceleration_maf_y(max_index_y), 'rs', 'Markersize', 10);
            end
        elseif(acceleration_maf_y(i) < -0.25)
            if (previous_acc_y < current_acc_y)&&(min_index_y == 0)
                min_index_y = i - 1
                plot(min_index_y*0.02, acceleration_maf_y(min_index_y), 'rs', 'Markersize', 10);
            end
        else
            max_index_y = 0;
            min_index_y = 0;
        end
    end
    
%     saveas(gcf, strcat('C:\Users\XRQ\Desktop\Hand Gesture Recognition\program\gesture_segmentation\figures\', files(i).name), 'bmp'); 
%     close;
    clear acceleration_maf_x acceleration_maf_y sample_time;
end